package cn.vcjf.washcar.api.service.controller;


import cn.vcjf.washcar.api.order.entity.WashCarOrder;
import cn.vcjf.washcar.api.order.service.WashCarOrderService;
import cn.vcjf.washcar.api.service.entity.ServerInfo;
import cn.vcjf.washcar.api.service.service.ServerInfoService;
import cn.vcjf.washcar.api.user.service.UserService;
import cn.vcjf.washcar.common.web.TableResult;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 服务种类信息表 前端控制器
 * </p>
 *
 * @author zhengzihang
 * @since 2018-07-20
 */
@Controller
@RequestMapping("/service/serverInfo")
public class ServerInfoController {
	@Resource
	private ServerInfoService serverInfoService;
	@Resource
	private UserService userService;
	@Resource
	private WashCarOrderService washCarOrderService;

	@Value("${service.normal.type}")
	private Integer serviceNormalType;

	@Value("${service.discounts.type}")
	private Integer serviceDiscountsType;

	@RequestMapping("/addServerInfo")
	@ResponseBody
	public TableResult addServerInfo(ServerInfo serverInfo) {
		serverInfo.preInsert();
		boolean b = serverInfoService.insert(serverInfo);
		return TableResult.Result(b);
	}

	@RequestMapping("/updateServerInfo")
	@ResponseBody
	public TableResult updateServerInfo(ServerInfo serverInfo) {
		serverInfo.preUpdate();
		boolean b = serverInfoService.updateById(serverInfo);
		return TableResult.Result(b);
	}

	@RequestMapping("/findServerInfoOne")
	@ResponseBody
	public TableResult findServerInfoOne(ServerInfo serverInfo) {

		EntityWrapper<ServerInfo> wrapper = new EntityWrapper<ServerInfo>();

		if (serverInfo.getId() != null) {
			wrapper.eq("id", serverInfo.getId());
		}
		//Todo

		ServerInfo result = serverInfoService.selectOne(wrapper);

		return TableResult.Result(result);
	}

	@RequestMapping("/findServerInfoList")
	@ResponseBody
	public TableResult findServerInfoList(ServerInfo serverInfo,String userId,String all) {
		EntityWrapper<ServerInfo> query = new EntityWrapper<>();
		if (StringUtils.isBlank(all)){
			EntityWrapper<WashCarOrder> wrapper = new EntityWrapper<>();
			wrapper.eq("userId", userId);
			List<WashCarOrder> serverInfos = washCarOrderService.selectList(wrapper);

			if(serverInfos==null||serverInfos.isEmpty()){
				//有优惠
				query.eq("type", serviceDiscountsType);
			}else {
				//无优惠
				query.eq("type", serviceNormalType);
			}
		}
		if (serverInfo.getId() != null) {
			query.eq("id", serverInfo.getId());
		}
		//Todo
		query.orderBy("type");

		Page<ServerInfo> page = new Page<ServerInfo>(serverInfo.getPage(), serverInfo.getLimit());

		Page<ServerInfo> result = serverInfoService.selectPage(page, query);

		return TableResult.Result(result);

	}

	@RequestMapping("/deleteServerInfo")
	@ResponseBody
	public TableResult deleteServerInfo(ServerInfo serverInfo) {
		serverInfo.preUpdate();
		boolean b = serverInfoService.deleteById(serverInfo.getId());
		return TableResult.Result(b);
	}

}
